<template>
  <div>
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="气瓶编号" >
        <el-input v-model="queryParams.productNo" placeholder="请输入气瓶编号" clearable size="small"
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="进入时间">
        <el-date-picker
          v-model="dateRange"
          size="small"
          style="width: 240px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item label="气瓶状态">
        <el-select v-model="queryParams.status" placeholder="请选择气瓶类型" clearable size="small">
          <el-option v-for="dict in statusOptions" :key="dict.dictValue" :label="dict.dictLabel"
            :value="dict.dictValue" />
        </el-select>
      </el-form-item>
      <el-form-item label="负责人员" >
        <el-input v-model="queryParams.ownerNo" placeholder="请输入负责人工号或姓名" clearable size="small"
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item>
        <el-button type="cyan" icon="el-icon-search" size="small"  @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="small"   @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <el-table v-loading="loading" :data="cylinderList" >
      <el-table-column label="二维码" align="center" prop="productNo" width="160" :show-overflow-tooltip="true"/>
      <el-table-column label="使用单位"  prop="deptName" :show-overflow-tooltip="true"/>
      <el-table-column label="状态" align="center" prop="status" :formatter="cylinderStatusFormat" width="60">
      </el-table-column>
      <el-table-column label="气瓶类型" align="center" prop="productGasType" :formatter="productGasTypeFormat" :show-overflow-tooltip="true"/>
      <el-table-column label="进入时间" align="center" prop="enteredAt" width="120">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.enteredAt, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column>
      <el-table-column label="离开时间" align="center" prop="leftAt" width="120">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.leftAt, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column>
      <el-table-column label="负责人" align="center" prop="ownerNo" width="80"/>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />
    </el-col>
    </el-row>
    <!-- 添加或修改气瓶对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-form-item label="编号" prop="productNo">
          <el-input v-model="form.productNo" placeholder="请输入备品编号" class="input_dialog_w" />
        </el-form-item>
        <el-form-item label="使用单位" prop="deptName">
          <el-input v-model="form.deptName" disabled class="input_dialog_w" />
        </el-form-item>
        <el-form-item label="负责人工号" prop="ownerNo">
          <el-input v-model="form.ownerNo" placeholder="请输入负责人工号" class="input_dialog_w" @blur="searchUser"
            @keyup.enter.native="searchUser" />
        </el-form-item>
        <el-form-item label="负责人姓名" prop="ownerName">
          <el-input v-model="form.ownerName" placeholder="负责人姓名" disabled class="input_dialog_w" />
        </el-form-item>
        <el-form-item label="气瓶类型" prop="productGasType">
          <el-select v-model="form.productGasType" placeholder="请选择气瓶类型" class="input_dialog_w">
            <el-option v-for="dict in productGasTypeOptions" :key="dict.dictValue" :label="dict.dictLabel"
              :value="dict.dictValue"></el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="进入时间" prop="enteredAt">
          <el-date-picker clearable size="small" v-model="form.enteredAt" type="date"
            value-format="yyyy-MM-dd" placeholder="选择进入时间">
          </el-date-picker>
        </el-form-item>

        <el-form-item label="离开时间" prop="leftAt" v-if="form.id>0">
          <el-date-picker clearable size="small" v-model="form.leftAt" type="date"
            value-format="yyyy-MM-dd" placeholder="选择离开时间">
          </el-date-picker>
        </el-form-item>


      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import {
    listCylinder,
  } from "@/api/labbase/cylinder";
  import {
    getAllGasCylindersType
  } from "@/api/labdata/gasCylinderNum";
  export default {
    name: "Cylinder",
    components: {},
    data() {
      return {
        // 日期范围
        dateRange: [],
        // 遮罩层
        loading: true,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        // 气瓶表格数据
        cylinderList: [],
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        // 气瓶类型字典
        productGasTypeOptions: [],
        // 使用状态字典
        statusOptions: [],
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
        },
        // 表单参数
        form: {},
        // 表单校验
        rules: {},
      };
    },
    props: {
      deptData: {
        type: Object,
        default: () => ({})
      },
    },
    watch: {
      deptData: function(newVal, oldVal) {
        this.queryParams.deptId = newVal.id;
        this.getList();
      },
    },
    created() {
      if(this.deptData){
        this.canAdd = this.deptData.orgFunctionOpt==4
      }
      this.getAllGasType();
      this.getDicts("cylinderStatusOpt").then(response => {
        this.statusOptions = response.data;
      });
       this.getList();
    },
    methods: {
      /** 查询气瓶列表 */
      getList() {
        this.loading = true;
        listCylinder(this.addDateRange(this.queryParams, this.dateRange)).then((response) => {
          this.cylinderList = response.rows;
          this.total = response.total;
          this.loading = false;
        });
      },
      getAllGasType() {
        getAllGasCylindersType().then(response => {
          let items = response.data;
          items.forEach(item => {
            this.productGasTypeOptions.push({
              dictValue: item.cylindersTypeId,
              dictLabel: item.name,
            });
          });
        });
      },
      // 气瓶类型字典翻译
      productGasTypeFormat(row, column) {
        return this.selectDictLabel(
          this.productGasTypeOptions,
          row.productGasType
        );
      },
      // 使用状态字典翻译
      cylinderStatusFormat(row, column) {
        return this.selectDictLabels(this.statusOptions, row.status);
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.queryParams = {
          pageNum: 1,
          pageSize: 10,};
        this.resetForm("queryForm");
        this.handleQuery();
      },

    },
  };
</script>
<style type="text/css" scoped>
  .input_dialog_w {
    width: 240px;
  }
</style>
